﻿using System;
using System.Collections.Generic;
using System.Linq;
using XXTBaseService.XXTWebService;

namespace XXTBaseService
{
    public static class DataSync
    {
        private static xxtInter inter;
        static DataSync() 
        {
            inter = new xxtInter();
        }
        /// <summary>
        /// 创建消息头
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        private static HeadMessage CreateHead(FunCode code)
        {
            HeadMessage headMsg = new HeadMessage();
            headMsg.functionCode = ((int)code).ToString().PadLeft(3, '0');
            headMsg.transId = string.Format("SI{0}", DateTime.Now.ToString("yyyyMMddHHmmssffffff"));
            headMsg.siId = ConstVlaue.SIID;
            headMsg.schoolId = ConstVlaue.SCHOOLID;
            headMsg.timeStamp = DateTime.Now.ToString("yyyyMMddHHmmssffff");
            return headMsg;
        }
        /// <summary>
        /// 教师信息同步
        /// </summary>
        /// <param name="actionCode"></param>
        /// <returns></returns>
        public static ErrorMsg TeacherDataSync(ActionCode actionCode)
        {
            //teaid 209CD074-D077-429A-AE9A-9F8A9B95123F
            TeacherRequestMessage trm = new TeacherRequestMessage();
            trm.HEAD = CreateHead(FunCode.教师资料变更);
            TeacherRequestMessageBody trmb = new TeacherRequestMessageBody();
            trmb.teacherId = "209CD074-D077-429A-AE9A-9F8A9B95123F";
            trmb.teaName = "Teacher1";
            trmb.teaPhone = "18268819487";
            trmb.role = "5";
            trmb.classNames = "1班";
            trmb.icNo = "";
            trmb.actionCode = ((int)actionCode).ToString();
            trm.BODY = trmb;
            var result = inter.TeaDataSync(trm);
            var error = Error.CheckError(result.BODY.status);
            if (error != null)
            {
                error.Remark = result.BODY.remark;
            }
            return error;
        }
        /// <summary>
        /// 学生信息同步
        /// </summary>
        /// <param name="actionCode"></param>
        /// <returns></returns>
        public static ErrorMsg StudentDataSync(ActionCode actionCode)
        {
            //stuid 92FB4849-0B28-4D99-A87A-000F5AB02469
            StudentRequestMessage srm = new StudentRequestMessage();
            srm.HEAD = CreateHead(FunCode.学生资料变更);
            StudentRequestMessageBody srmb = new StudentRequestMessageBody();
            srmb.studentId = "137700008684";
            srmb.stuAME = "卢钇名";
            srmb.className = "1班";
            srmb.grade = "7";
            srmb.icNo = "";
            srmb.actionCode = ((int)actionCode).ToString();
            srm.BODY = srmb;

            var result = inter.StudentDataSync(srm);
            var error = Error.CheckError(result.BODY.status);
            if (error != null)
            {
                error.Remark = result.BODY.remark;
            }
            return error;
        }
        /// <summary>
        /// 家庭信息同步
        /// </summary>
        /// <param name="actionCode"></param>
        /// <returns></returns>
        public static ErrorMsg FamilyDataSync(ActionCode actionCode)
        {
            FamilyRequestMessage frm = new FamilyRequestMessage();
            frm.HEAD = CreateHead(FunCode.家长资料变更);
            FamilyRequestMessageBody frmb = new FamilyRequestMessageBody();
            frmb.familyId = "AA1BA676-6C33-4876-B3BD-88ABFE3DE523";
            frmb.famName = "XXX";
            frmb.studentId = "92FB4849-0B28-4D99-A87A-000F5AB02469";
            frmb.relationship = "1";
            frmb.phone = "110";
            frmb.kfphone = "";
            frmb.icNo = "";
            frmb.bindPhones = "";
            frmb.actionCode = ((int)actionCode).ToString();
            frm.BODY = frmb;
            var result = inter.FamilyDataSync(frm);
            var error = Error.CheckError(result.BODY.status);
            if (error != null)
            {
                error.Remark = result.BODY.remark;
            }
            return error;
        }
        /// <summary>
        /// 订购关系变更
        /// </summary>
        /// <param name="actionCode"></param>
        /// <returns></returns>
        public static ErrorMsg OrderDataSync(OrderActionCode actionCode) 
        {
            OrderRequestMessage orm = new OrderRequestMessage();
            orm.HEAD = CreateHead(FunCode.订购关系变更);
            OrderRequestMessageBody ormb = new OrderRequestMessageBody();
            ormb.familyId = "";
            ormb.serviceCode = "";
            ormb.charge = "";
            ormb.actionCode = ((int)actionCode).ToString();
            orm.BODY = ormb;
            var result = inter.OrderDataSync(orm);
            var error = Error.CheckError(result.BODY.status);
            if (error != null) 
            {
                error.Remark = result.BODY.remark;
            }
            return error;
        }
        /// <summary>
        /// 短信下行请求
        /// </summary>
        /// <param name="smsType"></param>
        /// <param name="successIds">发送成功的ID</param>
        /// <returns></returns>
        public static ErrorMsg Downsms(DownSmsType smsType,List<string> successIds) 
        {
            DownsmsRequestMessage drm = new DownsmsRequestMessage();
            drm.HEAD = CreateHead(FunCode.短信下行);
            DownsmsRequestMessageBody drmb = new DownsmsRequestMessageBody();
            drmb.teacherId = "1600141";
            drmb.ids = "1068689";
            drmb.content = "这是一个测试短信";
            drmb.smstype = ((int)smsType).ToString();
            drmb.isLong = "0";
            drm.BODY = drmb;
            var result = inter.Downsms(drm);
            successIds = result.BODY.ids.Split(',').ToList();
            var error = Error.CheckError(result.BODY.status);
            if (error != null) 
            {
                error.Remark = result.BODY.remark;
            }
            return error;
        }
        /// <summary>
        /// 短信上行请求
        /// </summary>
        /// <param name="smsType"></param>
        /// <returns></returns>
        public static ErrorMsg Upsms(UpSmsType smsType) 
        {
            UpsmsRequestMessage urm = new UpsmsRequestMessage();
            urm.HEAD = CreateHead(FunCode.短信上行);
            UpsmsRequestMessageBody urmb = new UpsmsRequestMessageBody();
            urmb.smstype = ((int)smsType).ToString();
            urmb.pageSize = "50";
            urmb.pageSize = "3";
            urmb.statTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            urm.BODY = urmb;
            var result = inter.Upsms(urm);
            var error = Error.CheckError(result.BODY.status);
            if (error != null) 
            {
                error.Remark = result.BODY.remark;
            }
            return error;
        }
    }
}
